#!/bin/bash

echo "开始安装......"

# 更新yum
# yum update

# 虚拟机需导入密钥
# 查看系统版本信息 从 网易开源镜像站 mirrors.163.com. 找到系统对应密钥
# cat /etc/redhat-release 
rpm --import http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

# 安装并启动docker
# 使用阿里云镜像 安装社区版

# 卸载旧的版本
sudo yum remove docker  docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
				  
# 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# 添加软件源信息
# 阿里源
# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 官网源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 更新并安装 Docker-CE(社区版)
sudo yum makecache fast

# 安装docker-ce
sudo yum install -y docker-ce docker-ce-cli containerd.io

# 启动docker
sudo systemctl start docker

# 开机启动
systemctl enable docker

# 查看docker version
docker -v

echo "已经成功安装docker"
echo "=================="

# 安装zip
yum install -y unzip zip 

echo "打开防火墙端口"
# nginx端口
# sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
# sudo firewall-cmd --permanent --zone=public --add-port=443/tcp
sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
# mysql端口
sudo firewall-cmd --permanent --zone=public --add-port=3306/tcp
# redis端口
# sudo firewall-cmd --permanent --zone=public --add-port=6379/tcp

sudo firewall-cmd --reload
echo "打开防火墙端口结束"


DIR="$( cd "$( dirname "$0"  )" && pwd  )"

echo "nginx安装"

mkdir -p "${DIR}/config/nginx"
mkdir -p "${DIR}/config/nginx/log"
mkdir -p "${DIR}/config/nginx/conf.d"

docker pull nginx

# 复制配置文件
# cp -r /etc/nginx/nginx.conf "${DIR}/config/nginx"
# cp -r /etc/nginx/conf.d/default.conf "${DIR}/config/nginx/conf.d"
# cp -r /var/log/nginx "${DIR}/config/nginx/log/nginx"

docker run --name nginx -p 80:80 -v "${DIR}/config/nginx/nginx.conf":/etc/nginx/nginx.conf -v "${DIR}/config/nginx/log/nginx":/var/log/nginx -v "${DIR}/config/nginx/conf.d/default.conf":/etc/nginx/conf.d/default.conf -d nginx

echo "nginx结束"



echo "mysql安装"
docker pull mysql:5.7

mkdir -p "${DIR}/config/mysql/data"
mkdir -p "${DIR}/config/mysql/logs"
mkdir -p "${DIR}/config/mysql/conf"

docker run -p 3306:3306 --name mysql -v "${DIR}/config/mysqll/conf":/etc/mysql/conf.d -v "${DIR}/config/mysqll/logs":/logs -v "${DIR}/config/mysqll/data":/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
echo "mysql结束"



echo "redis安装"

docker pull redis

mkdir -p "${DIR}/config/redis/data"
mkdir -p "${DIR}/config/redis/conf"
# 复制配置文件
# cp -r /etc/config/redis/redis.conf "${DIR}/redis/conf"

docker run -d --name redis -p 6379:6379 --restart unless-stopped -v "${DIR}/config/redis/data":/data -v "${DIR}/config/redis/conf/redis.conf":/etc/redis/redis.conf redis-server /etc/redis/redis.conf redis

echo "redis结束"

echo "修改 ${DIR}/redis/conf/redis.conf"
echo "appendonly yes     =>  启动Redis持久化功能 (默认 no , 所有信息都存储在内存 [重启丢失] 。 设置为 yes , 将存储在硬盘 [重启还在])"
echo "protected-mode no	 =>  关闭protected-mode模式，此时外部网络可以直接访问 (docker貌似自动开启了)"
echo "bind 0.0.0.0	     =>  设置所有IP都可以访问 (docker貌似自动开启了)"
echo "requirepass 密码	 =>  设置密码"



